package cn.stylefeng.guns.modular.rest;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.format.FastDateFormat;
import cn.stylefeng.guns.comm.util.DateUtils;
import cn.stylefeng.guns.modular.school.entity.ParkingInfo;
import cn.stylefeng.guns.modular.school.entity.ParkingPlaceInfo;
import cn.stylefeng.guns.modular.school.entity.ParkingPlaceUseInfo;
import cn.stylefeng.guns.modular.school.model.params.ParkingInfoParam;
import cn.stylefeng.guns.modular.school.model.result.ParkingInfoResult;
import cn.stylefeng.guns.modular.school.model.result.ParkingPlaceInfoResult;
import cn.stylefeng.guns.modular.school.service.ParkingInfoService;
import cn.stylefeng.guns.modular.school.service.ParkingPlaceInfoService;
import cn.stylefeng.guns.modular.school.service.ParkingPlaceUseInfoService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 绑定控制器
 *
 * @author
 * @Date 2019-11-18 13:24:11
 */
@RestController
@RequestMapping("/gunsApi/parking")
public class ApiNeedTokenParkingController extends BaseController {

    @Autowired
    private ParkingInfoService parkingInfoService;
    @Autowired
    private ParkingPlaceInfoService parkingPlaceInfoService;
    @Autowired
    private ParkingPlaceUseInfoService parkingPlaceUseInfoService;

    @RequestMapping(value = "detail")
    public Object detail(Integer seqId, String openId) {
        Map map = new HashMap();
        String now = DateUtil.now();
        String hHmm = DateUtil.format(new DateTime(), "HHmm");

        ParkingInfo parkingInfo = parkingInfoService.getById(seqId);
        List<ParkingPlaceInfo> parkingPlaceInfoList = parkingPlaceInfoService.list(new QueryWrapper<ParkingPlaceInfo>().eq("parking_id", parkingInfo.getSeqId()));
        List<ParkingPlaceInfoResult> list = new ArrayList<>();
        // 不能使用的车位
        List<ParkingPlaceInfoResult> canNotUserlist = new ArrayList<>();

        parkingPlaceInfoList.forEach(parkingPlaceInfo -> {
            ParkingPlaceInfoResult parkingPlaceInfoResult = new ParkingPlaceInfoResult();
            ToolUtil.copyProperties(parkingPlaceInfo, parkingPlaceInfoResult);
            String startHHmm = DateUtil.format(parkingPlaceInfo.getTimeStart(), "HH:mm");
            String endHHmm = DateUtil.format(parkingPlaceInfo.getTimeEnd(), "HH:mm");
            if (startHHmm == null) {
                if (!(Integer.parseInt(hHmm) >= Integer.parseInt("0000") && Integer.parseInt(hHmm) <= Integer.parseInt(DateUtil.format(parkingPlaceInfo.getTimeEnd(), "HHmm")))) {
                    parkingPlaceInfoResult.setStatus("3");
                    canNotUserlist.add(parkingPlaceInfoResult);
                }
            } else {
                if (!(Integer.parseInt(hHmm) >= Integer.parseInt(DateUtil.format(parkingPlaceInfo.getTimeStart(), "HHmm")) && Integer.parseInt(hHmm) <= Integer.parseInt(DateUtil.format(parkingPlaceInfo.getTimeEnd(), "HHmm")))) {
                    parkingPlaceInfoResult.setStatus("3");
                    canNotUserlist.add(parkingPlaceInfoResult);
                }
            }

            parkingPlaceInfoResult.setTimeStartStr(startHHmm == null ? "00:00" : startHHmm);
            parkingPlaceInfoResult.setTimeEndStr(endHHmm);
            list.add(parkingPlaceInfoResult);
        });
        int free = parkingPlaceInfoService.count(new QueryWrapper<ParkingPlaceInfo>().eq("status", "0").eq("parking_id", parkingInfo.getSeqId()));
        map.put("parkingInfo", parkingInfo);
        map.put("free", free - canNotUserlist.size());//抛去不能使用的车位才是空闲车位
        map.put("parkingPlaceInfoList", list);
        map.put("now", FastDateFormat.getInstance("HH:mm").format(new DateTime()));
        map.put("end", DateUtils.addDateMinut(now, 1).substring(10, 16));


        ParkingPlaceUseInfo parkingPlaceUseInfo = parkingPlaceUseInfoService.getOne(new QueryWrapper<ParkingPlaceUseInfo>().eq("open_id", openId).eq("pay_flag", 1));
        map.put("parkingPlaceUseInfo", parkingPlaceUseInfo);

        // 计算需要支付的价格
        if (parkingPlaceUseInfo != null) {
            int hour = DateUtils.distanceHour(DateUtils.getSystemDate(), parkingPlaceUseInfo.getCreateTime());
            ParkingPlaceInfo parkingPlaceInfo = parkingPlaceInfoService.getById(parkingPlaceUseInfo.getParkingPlaceId());
            int needPayPrice = parkingPlaceInfo.getPrice() * hour;
            map.put("needPayPrice", needPayPrice == 0 ? parkingPlaceInfo.getPrice() : needPayPrice);

        }

        SUCCESS_TIP.setData(map);
        return SUCCESS_TIP;
    }


    @RequestMapping(value = "list")
    public Object list(ParkingInfoParam parkingInfoParam) {
        Map map = new HashMap();
        String hHmm = DateUtil.format(new DateTime(), "HHmm");
        List<ParkingInfoResult> parkingInfoResults = new ArrayList<>();

        LambdaQueryWrapper<ParkingInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(parkingInfoParam.getName())) {
            lambdaQueryWrapper.like(ParkingInfo::getName, parkingInfoParam.getName());
        }
        List<ParkingInfo> parkingInfos = parkingInfoService.list(lambdaQueryWrapper);

        parkingInfos.forEach(parkingInfo -> {
            ParkingInfoResult parkingInfoResult = new ParkingInfoResult();
            parkingInfoResults.add(parkingInfoResult);
            ToolUtil.copyProperties(parkingInfo, parkingInfoResult);
            int total = parkingPlaceInfoService.count(new QueryWrapper<ParkingPlaceInfo>().eq("parking_id", parkingInfo.getSeqId()));
            int free = parkingPlaceInfoService.count(new QueryWrapper<ParkingPlaceInfo>().eq("status", "0").eq("parking_id", parkingInfo.getSeqId()));
            parkingInfoResult.setFree(free);

            List<ParkingPlaceInfo> list = parkingPlaceInfoService.list(new QueryWrapper<ParkingPlaceInfo>().eq("status", "0").eq("parking_id", parkingInfo.getSeqId()));
            list.forEach(parkingPlaceInfo -> {
                if (parkingPlaceInfo.getTimeStart() == null) {
                    if (!(Integer.parseInt(hHmm) >= Integer.parseInt("0000") && Integer.parseInt(hHmm) <= Integer.parseInt(DateUtil.format(parkingPlaceInfo.getTimeEnd(), "HHmm")))) {
                        parkingInfoResult.setFree(parkingInfoResult.getFree() - 1);
                    }
                } else {
                    if (!(Integer.parseInt(hHmm) >= Integer.parseInt(DateUtil.format(parkingPlaceInfo.getTimeStart(), "HHmm")) && Integer.parseInt(hHmm) <= Integer.parseInt(DateUtil.format(parkingPlaceInfo.getTimeEnd(), "HHmm")))) {
                        parkingInfoResult.setFree(parkingInfoResult.getFree() - 1);
                    }
                }

            });

            parkingInfoResult.setTotal(total);

        });

        map.put("parkingInfos", parkingInfoResults);

        SUCCESS_TIP.setData(map);
        return SUCCESS_TIP;
    }

    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ParkingInfoParam parkingInfoParam) {
        this.parkingInfoService.update(parkingInfoParam);
        return ResponseData.success();
    }

    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ParkingInfoParam parkingInfoParam) {
        ParkingInfo entity = new ParkingInfo();
        ToolUtil.copyProperties(parkingInfoParam, entity);
        this.parkingInfoService.save(entity);

        ParkingPlaceInfo parkingPlaceInfo = new ParkingPlaceInfo();
        parkingPlaceInfo.setStatus("0");
        parkingPlaceInfo.setLocation("001");
        parkingPlaceInfo.setParkingId(entity.getSeqId());
        parkingPlaceInfoService.save(parkingPlaceInfo);

        return ResponseData.success();
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ParkingInfoParam parkingInfoParam) {
        this.parkingInfoService.delete(parkingInfoParam);
        return ResponseData.success();
    }
}


